package com.feiyuan.core.service.impl;

import com.feiyuan.common.utils.ObjectMapperUtils;
import com.feiyuan.core.pojo.entity.AdminMenu;
import com.feiyuan.core.mapper.AdminMenuMapper;
import com.feiyuan.core.pojo.entity.AdminUser;
import com.feiyuan.core.service.AdminMenuService;
import com.feiyuan.core.service.AdminRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xupf
 * @since 2022-09-06
 */
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuMapper, AdminMenu> implements AdminMenuService {

    @Autowired
    private AdminMenuMapper adminMenuMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private AdminRoleMenuService roleMenuService;

    @Override
    public List<AdminMenu> selectMenus() {
        // 1.查询所有的一级菜单
        List<AdminMenu> menuAll = adminMenuMapper.selectList(null);

        // 2.递归查询二级或者三级菜单
        List<AdminMenu> collect = menuAll.stream().filter((menu) -> {
            return menu.getParentId() == 0;
        }).map((menu) -> {
            List<AdminMenu> children = this.getChildren(menu, menuAll);
            if (children != null && children.size() > 0) {
                menu.setChildren(children);
            } else {
                menu.setChildren(null);
            }
//            menu.setChildren(this.getChildren(menu, menuAll));
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<AdminMenu> selectUpMenus() {
        // 1.查询所有的列表
        List<AdminMenu> menuAll = adminMenuMapper.selectList(new QueryWrapper<AdminMenu>().ne("menu_type", 2));
        List<AdminMenu> all = new ArrayList<>();
        AdminMenu adminMenu = new AdminMenu();
        adminMenu.setId(0l);
        adminMenu.setMenuTitle("一级目录");

        List<AdminMenu> collect = menuAll.stream().filter((menu) -> {
            return menu.getParentId() == 0;
        }).map((menu) -> {
            List<AdminMenu> children = this.getChildren(menu, menuAll);
            if (children != null && children.size() > 0) {
                menu.setChildren(children);
            } else {
                menu.setChildren(null);
            }
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());
        if (collect != null && collect.size() > 0) {
            adminMenu.setChildren(collect);
        } else {
            adminMenu.setChildren(null);
        }
        all.add(adminMenu);

        return all;
    }

    @Override
    public List<AdminMenu> selectNavigations(String adminToken) {
        // 1.通过adminToken得到用户信息
        String menuStr = redisTemplate.opsForValue().get("ADMIN_USER_" + adminToken);
        AdminUser adminUser = ObjectMapperUtils.readValue(menuStr, AdminUser.class);

        // 2.判断用户是否是超级管理员
        List<String> roles = adminUser.getRoles();
        List<AdminMenu> menus = new ArrayList<>();
        if (roles.contains("SYSADMIN")) {
            // 查询所有的菜单
            menus = this.baseMapper.selectList(null);
        } else {
            // 根据用户对应的角色查询出对应的菜单
            menus = this.baseMapper.selectMenus(adminUser.getId());

        }
        List<AdminMenu> finalMenus = menus;
        List<AdminMenu> collect = menus.stream().filter(item -> {
            return item.getParentId() == 0;
        }).map(menu -> {
            menu.setChildren(this.getNaviChildren(menu, finalMenus, adminUser.getId()));
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 查询左侧菜单的子类
     * @param menu
     * @param menus
     * @return
     */
    private List<AdminMenu> getNaviChildren(AdminMenu menu, List<AdminMenu> menus, Long adminUserId) {
        List<AdminMenu> collect = menus.stream().filter(item -> {
            return item.getParentId() == menu.getId() && item.getMenuType() == 1;
        }).map(item -> {
            item.setChildren(this.getNaviChildren(item, menus, adminUserId));
            item.setPerms(roleMenuService.selectUserMenus(adminUserId));
            return item;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取查询列表中的子菜单
     * @param root
     * @param all
     * @return
     */
    private List<AdminMenu> getChildren(AdminMenu root, List<AdminMenu> all) {
        List<AdminMenu> collect = all.stream().filter((menu) -> {
            return menu.getParentId() == root.getId();
        }).map((menu) -> {
            List<AdminMenu> children = this.getChildren(menu, all);
            if (children != null && children.size() > 0) {
                menu.setChildren(children);
            } else {
                menu.setChildren(null);
            }
            return menu;
        }).sorted((menu1, menu2) -> {
            return menu1.getMenuSort() - menu2.getMenuSort();
        }).collect(Collectors.toList());
        return collect;
    }
}
